package com.lhj.es.service;

import com.lhj.es.common.HelloRetryException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author linghaijin
 * @date 2021-04-22 10:41
 */
@Slf4j
@Service
public class HelloService implements IHelloService {

    private static AtomicLong helloTimes = new AtomicLong();

    @Retryable(maxAttempts = 4)
    @Override
    public String hello() {
        long times = helloTimes.incrementAndGet();
        if (times % 4 != 0) {
            log.warn("发生异常，time：{}", LocalTime.now() );

            throw new HelloRetryException("发生hello异常");
        }

        return "hello";
    }

}
